import O1HD from "../engines/gameEngine";
import gameConfig from "../engines/configs/gameConfig";
import mainRoot from "../Game/MainRoot";
import physics_tool from "../engines/libs/physics_tool";

const { ccclass, property } = cc._decorator;

function beforeGameRun() {
  // 抗锯齿 要在cc.game.run前调用 webgl 才生效
  cc.macro.ENABLE_WEBGL_ANTIALIAS = true;
  // 是否开启多点触摸
  cc.macro.ENABLE_MULTI_TOUCH = true;
  //
  // console.log("引擎配置初始化");
}

beforeGameRun();

@ccclass
export default class start extends cc.Component {
  // LIFE-CYCLE CALLBACKS:

  onLoad() {
    let uiRoot: cc.Node = this.node.parent.getChildByName("UIRoot");
    cc.game.addPersistRootNode(uiRoot);
    mainRoot.SetUIRoot(uiRoot);
  }

  start() {
    //日志服最先
    if (gameConfig.LOG_SERVICE_OPEN) {
      O1HD.getServices()
        .logSrv.loadService()
        .then(() => {
          O1HD.getServices()
            .logSrv.onLoadedService()
            .then(() => {
              this.onReady();
            });
        });
    } else {
      this.onReady();
    }
  }

  protected onReady() {
    // console.log("<start> 场景");
    if (!cc.sys.isNative) {
      console.log("cocos webgl");
    } else {
      console.log("cocos 2d-x");
    }
    //初始化cocos引擎配置
    this.initEngineConfig();
    //打印cocos引擎配置
    // this.printEngineConfig();
    //
    this.goLoading();
  }

  private initEngineConfig() {
    cc.debug.setDisplayStats(false);
    physics_tool.openCollision(false);
    physics_tool.openPhysics(true);
    let manager = cc.director.getCollisionManager();
    manager.enabled = true;
    manager.enabledDebugDraw = false;
    cc.director.getPhysicsManager().enabledAccumulator = true;

    // 物理步长，默认 FIXED_TIME_STEP 是 1/60

    cc.PhysicsManager.FIXED_TIME_STEP = 1 / 60;
  }

  //
  private printEngineConfig() {
    console.group("cocos引擎配置");
    console.log(
      "是否开启抗锯齿",
      cc.view.isAntiAliasEnabled(),
      cc.macro.ENABLE_WEBGL_ANTIALIAS
    );
    console.log("是否开启多点触摸", cc.macro.ENABLE_MULTI_TOUCH);
    console.log("是否显示左下角调试信息", cc.debug.isDisplayStats());
    console.log(
      "是否开启瓦片地图的自动裁减功能",
      cc.macro.ENABLE_TILEDMAP_CULLING
    );
    console.log("单次批处理渲染的顶点数量", cc.macro.BATCH_VERTEX_COUNT);
    console.log(
      "Canvas背景是否支持 alpha 通道",
      cc.macro.ENABLE_TRANSPARENT_CANVAS
    );
    console.log("引擎支持的图片格式", ...cc.macro.SUPPORT_TEXTURE_FORMATS);
    console.groupEnd();
  }

  private goLoading() {
    O1HD.loadScene("loading");
  }

  // update (dt) {}

}
